package ru.megawall;

import net.ericaro.surfaceplotter.JSurfacePanel;
import net.ericaro.surfaceplotter.Mapper;
import net.ericaro.surfaceplotter.ProgressiveSurfaceModel;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


/**
 * @author User #1
 */
public class Main extends JPanel {
    private ProgressiveSurfaceModel model;

    public void setMapper(Mapper mapper){
        model.setMapper(mapper);
        model.plot().execute();
        surfacePanel1.repaint();
    }

    public Main() {
        initComponents();

        //DefaultSurfaceModel model = new DefaultSurfaceModel();
        model = new ProgressiveSurfaceModel();
        surfacePanel1.setModel(model);

        model.setMapper(new Mapper() {
            public float f1(float x, float y) {
                float r = x + y;

                if (r == 0) return 1f;
                return (float) (Math.sin(r) / (r));
            }

            public float f2(float x, float y) {
                return (float) (Math.sin(x * y));
            }
        });
        model.plot().execute();
    }

    private void button1ActionPerformed() {
        JFileChooser jfc = new JFileChooser();
        try {
            if (jfc.showSaveDialog(surfacePanel1) == JFileChooser.APPROVE_OPTION)
                surfacePanel1.getSurface().doExportSVG(jfc.getSelectedFile());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        JFrame jf = new JFrame("sample");

        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
        Main sample = new Main();

        jf.getContentPane().add(sample);

        jf.pack();
        jf.setSize(500,500);
        for (int i = 0; i < 100; i++) {
            System.out.println(i);
            final float finalI = (float) (i*0.1);
            sample.setMapper(new Mapper() {
                public float f1(float x, float y) {
                    float r = x + y + finalI;

                    if (r == 0) return 1f;
                    return (float) (Math.sin(r) / (r));
                }

                public float f2(float x, float y) {
                    return (float) (Math.sin(x * y));
                }
            });
            jf.repaint();
            Thread.sleep(50);
        }
    }

    private void slider1StateChanged(ChangeEvent e) {
        if (!slider1.getValueIsAdjusting())
            model.setCurrentDefinition(slider1.getValue());
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        surfacePanel1 = new JSurfacePanel();
        toolBar1 = new JToolBar();
        button1 = new JButton();
        slider1 = new JSlider();

        //======== this ========
        setLayout(new BorderLayout());

        //---- surfacePanel1 ----
        surfacePanel1.setTitleText("title");
        surfacePanel1.setBackground(Color.white);
        surfacePanel1.setTitleFont(surfacePanel1.getTitleFont().deriveFont(surfacePanel1.getTitleFont().getStyle() | Font.BOLD, surfacePanel1.getTitleFont().getSize() + 6f));
        surfacePanel1.setConfigurationVisible(false);
        add(surfacePanel1, BorderLayout.CENTER);

        //======== toolBar1 ========
        {

            //---- button1 ----
            button1.setText("export SVG");
            button1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    button1ActionPerformed();
                }
            });
            toolBar1.add(button1);

            //---- slider1 ----
            slider1.setMaximum(6);
            slider1.setValue(0);
            slider1.setPaintTicks(true);
            slider1.setSnapToTicks(true);
            slider1.setMinorTickSpacing(1);
            slider1.setMajorTickSpacing(1);
            slider1.setPaintLabels(true);
            slider1.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    slider1StateChanged(e);
                }
            });
            toolBar1.add(slider1);
        }
        add(toolBar1, BorderLayout.NORTH);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JSurfacePanel surfacePanel1;
    private JToolBar toolBar1;
    private JButton button1;
    private JSlider slider1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}